package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.mapper.OrderDetailMapper;
import com.xmy.cultivate.mapper.RenewDataMapper;
import com.xmy.cultivate.mapper.StaffMapper;
import com.xmy.cultivate.mapper.StudentGradeMapper;
import com.xmy.cultivate.service.ICourseService;
import com.xmy.cultivate.service.IGradeService;
import com.xmy.cultivate.service.IRenewDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 续班表(最终方案) 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class RenewDataServiceImpl extends ServiceImpl<RenewDataMapper, RenewData> implements IRenewDataService {

    @Autowired
    RenewDataMapper renewDataMapper;

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    StudentGradeMapper studentGradeMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    ICourseService iCourseService;

    @Autowired
    IGradeService iGradeService;

    @Override
    public boolean updateTeacher(Long schoolId, Long studentId, Long courseId, Long gradeId){
        Course course = iCourseService.getById(courseId);
        Grade grade = iGradeService.getById(gradeId);
        RenewData renewData = this.getRenewDataForSubjects(schoolId,course.getSubjectsId(),studentId,grade.getYearPart(),course.getQuarterNum(),1);


        //为空表示没有需要续班的信息
        if(renewData != null){
            Staff staff = staffMapper.selectById(grade.getTeacherId());
            if((renewData.getTeacherId() == null || renewData.getTeacherId() ==0)){
                renewData.setTeacherId(grade.getTeacherId());
                renewData.setTeacherName(staff.getName());
                renewData.setYearClassId(grade.getYearClassId().getKey());
                renewDataMapper.updateById(renewData);
            }else if(renewData.getTeacherId().equals(grade.getTeacherId())==false){
                StudentGrade studentGrade = studentGradeMapper.getLastForStudent(schoolId,studentId,course.getSubjectsId(),grade.getYearPart(),course.getQuarterNum());
                if(studentGrade.getStudentId().equals(grade.getTeacherId())==false){
                    if(studentGrade.getUseCourseCount().equals(0)){
                        renewData.setTeacherId(grade.getTeacherId());
                        renewData.setTeacherName(staff.getName());
                        renewData.setYearClassId(grade.getYearClassId().getKey());
                    }else {
                        Integer yearAndQuarter = Integer.parseInt(grade.getYearPart() +"" +course.getQuarterNum());

                        Integer residueCount = orderDetailMapper.getResidueCourseCount(schoolId,studentId,course.getSubjectsId(),yearAndQuarter,course.getLessonType());
                        if(residueCount>2){
                            renewData.setTeacherId(grade.getTeacherId());
                            renewData.setTeacherName(staff.getName());
                            renewData.setYearClassId(grade.getYearClassId().getKey());
                        }
                    }
                }
            }
            RenewData renewData02 = this.getRenewDataForSubjects(schoolId,course.getSubjectsId(),studentId,grade.getYearPart(),course.getQuarterNum(),2);
            if((renewData02.getTeacherId() == null || renewData02.getTeacherId() ==0)){
                renewData02.setTeacherId(grade.getTeacherId());
                renewData02.setTeacherName(staff.getName());
                renewData02.setYearClassId(grade.getYearClassId().getKey());
                renewDataMapper.updateById(renewData02);
            }else if(renewData02.getTeacherId().equals(grade.getTeacherId())== false){

            }
        }
        return true;
    }

    /**
     * 获取有没有添加过报读的续班信息
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param yearPart
     * @param quarterNum
     * @param type
     * @return
     */
    public RenewData getRenewDataForSubjects(Long schoolId,Long subjectsId,Long studentId,Integer yearPart,Integer quarterNum,Integer type){
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",schoolId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("year_part",yearPart);////////////////////
        renewDataQueryWrapper.eq("quarter_num",quarterNum);
        renewDataQueryWrapper.eq("type",type);
        renewDataQueryWrapper.last("limit 1");
        RenewData renewData = renewDataMapper.selectOne(renewDataQueryWrapper);
        return  renewData;
    }
}
